<template>
  <div id="accountDiv">
    <div>
      <h3>账号设置</h3>
      <el-form ref="form" :model="form" label-position="left"
               label-width="120px">
        <el-form-item label="用户名">
          {{ form.username }}
        </el-form-item>
        <el-form-item label="角色">
          {{ form.grade === 0 ? "超级管理员" : form.grade === 1 ? "管理员" : "普通用户" }}
        </el-form-item>
        <el-form-item label="真实姓名">
          <el-input v-model="form.name" :value="form.name"></el-input>
        </el-form-item>
        <el-form-item label="修改登陆密码">
          <el-button type="primary" plain @click="upd">修改密码</el-button>
        </el-form-item>
        <el-form-item label="语言">
          <el-radio-group v-model="form.language">
            <el-radio label="1">中文</el-radio>
            <el-radio label="0">English</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="update">保存</el-button>
        </el-form-item>
      </el-form>

      <el-dialog
          title="修改登陆密码"
          :visible.sync="dialogValue"
          width="30%"
          :before-close="handleClose">
        <el-form ref="passwordForm" :model="passwordForm" label-position="left"
                 label-width="120px" :rules="rules">
          <el-form-item label="旧密码" prop="password">
            <el-input v-model="passwordForm.password" placeholder="旧密码"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword1">
            <el-input v-model="passwordForm.newPassword1" placeholder="新密码"></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="newPassword2">
            <el-input v-model="passwordForm.newPassword2" placeholder="新密码"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogValue = false">取 消</el-button>
          <el-button type="primary" @click="updatePassword">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {updateUser} from "@/api/user/login"
import axios from "axios";
export default {
  data() {
    return {
      user: JSON.parse(sessionStorage.getItem("user")),
      form: {},
      passwordForm: {
        password: "",
        newPassword1: "",
        newPassword2: ""
      },
      dialogValue: false,
      rules: {
        password: [
          {required: true, message: '请输入旧密码', trigger: 'blur'},
          {
            validator: this.validatePassword, // 使用自定义验证函数
            trigger: 'blur'
          }
        ],
        newPassword1: [
          {required: true, message: '请输入新密码', trigger: 'blur'},
          {min: 6, max: 14, message: '长度在 6 到 14 个字符', trigger: 'blur'}
        ],
        newPassword2: [
          {required: true, message: '请输入新密码', trigger: 'blur'},
          {
            validator: this.validateNewPassword, // 使用自定义验证函数
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.user = JSON.parse(sessionStorage.getItem("user"));
    if (!this.user) {
      this.$router.push({path: "/login"})
    } else {
      this.form = this.user;
      this.form.language = String(this.user.language);
      console.log(this.form);
    }
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？').then(() => {
        done();
      }).catch(() => {
      })
    },
    getList(){
      this.loading=true;
      axios.post("http://localhost:10060/advertising/list",this.queryParams).then(response =>{
        console.log(response)
        this.advertisingList = response.data.list;
        console.log(this.advertisingList)
        this.total = response.data.total;
        this.loading = false;
      })
    },
    update() {
      updateUser(this.form).then(res => {
        if (res.code == 200) {
          this.$message('修改成功，正在更新数据')
          sessionStorage.clear();
          sessionStorage.setItem("user", JSON.stringify(this.form));
          sessionStorage.setItem("id", JSON.stringify(this.form.userId));
          sessionStorage.setItem("name", JSON.stringify(this.form.name));
          sessionStorage.setItem("language", JSON.stringify(this.form.language));
          sessionStorage.setItem("grade", JSON.stringify(this.form.grade));
          setTimeout("location.reload()", 1000)
        } else {
          this.$message('修改失败!')
        }
      })
    },
    upd() {
      this.dialogValue = true
    },
    validateNewPassword(rule, value, callback) {
      if (value === '') {
        callback(new Error('请确认新密码'));
      } else if (value !== this.passwordForm.newPassword1) {
        callback(new Error('请确保两次密码一致'));
      } else {
        callback(); // 验证通过
      }
    },
    validatePassword(rule, value, callback) {
      if (value === '') {
        callback(new Error('请输入旧密码'));
      } else if (value !== this.user.password) {
        callback(new Error('旧密码错误'));
      } else {
        callback(); // 验证通过
      }
    },
    updatePassword() {
      this.form.password = this.passwordForm.newPassword1;
      this.update();
    }
  }
}
</script>

<style>
#accountDiv {
  display: flex;
  background-color: #f8f8f8;
  padding-left: 50px;
}
</style>